﻿Imports Microsoft.Office.Tools.Ribbon
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms




Public Class ArasRibbon



    Dim ArasUrl As String
    Dim ArasDB As String
    Dim ArasUser As String
    Dim ArasPass As String
    Dim UserLocalDir As String

    Dim ActiveDoc As Microsoft.Office.Interop.Excel.Workbook




    Private Sub NewDoc_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles NewDoc.Click

        'Ouverture d'une fenetre de création de document pour meta-données avant création du word




    End Sub

    Private Sub Open_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Open.Click

        'ouverture d'aras innovator avec la liste des documents disponibles

    End Sub

    Private Sub Lock_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Lock.Click

        ' lock du document, desactive le bouton lock et active le bouton unlock
        Dim result As String
        Dim ConnecAras As New ConnecteurAras(ArasUrl, ArasDB, ArasUser, ArasPass)
        result = ConnecAras.lockDoc(ItemNumber.Label)
        MsgBox(result)

        Unlock.Enabled = True
        Save.Enabled = True
        Lock.Enabled = False


    End Sub



    Private Sub ToggleButton1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs)





    End Sub

    Private Sub CreateFromActual_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles CreateFromActual.Click

        'Create a document with actual file attached + invite de commande pour méta données


    End Sub

    Private Sub GetInfo_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles GetInfo.Click


        ActiveDoc = Globals.ThisAddIn.Application.ActiveWorkbook
        'MsgBox(ActiveDoc.Variables.Count)

        If (ActiveDoc.Variables.Count > 0) Then
            ItemNumber.Label = ActiveDoc.Variables("ItemNumber").Value
            DocName.Label = "Name :" + ActiveDoc.Variables("DocName").Value
            DocNum.Label = "Doc Number :" + ActiveDoc.Variables("DocNum").Value
            Try
                DocClass.Label = "Class :" + ActiveDoc.Variables("DocClass").Value
            Catch ex As Exception
                DocClass.Label = "Class :" + "No Class"
            End Try


            ArasUrl = ActiveDoc.Variables("ArasUrl").Value
            ArasDB = ActiveDoc.Variables("ArasDB").Value
            ArasUser = ActiveDoc.Variables("ArasUser").Value
            ArasPass = ActiveDoc.Variables("ArasPass").Value
            UserLocalDir = ActiveDoc.Variables("UserLocalDir").Value


            'test if doc is locked
            Dim DocIsLocked As Boolean
            Dim ConnecAras As New ConnecteurAras(ArasUrl, ArasDB, ArasUser, ArasPass)
            DocIsLocked = ConnecAras.IsDocLocked(ItemNumber.Label)
            If DocIsLocked = True Then
                Save.Enabled = True
                Lock.Enabled = False
                UnLock.Enabled = True
                CloseDoc.Enabled = True
                CreateFromActual.Enabled = False
            Else
                Save.Enabled = False
                Lock.Enabled = True
                UnLock.Enabled = False
                CloseDoc.Enabled = True
                CreateFromActual.Enabled = False
            End If


           

        End If



    End Sub

    Private Sub Save_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Save.Click

        Dim result As String
        'uploader la nouvelle version du fichier
        Dim ConnecAras As New ConnecteurAras(ArasUrl, ArasDB, ArasUser, ArasPass)
        Dim FilepathComplete As String = UserLocalDir + "\" + ActiveDoc.Variables("DocName").Value + ".docx"

        ActiveDoc.SaveAs(FilepathComplete)

        'Check if document has already a file attached
        Dim hasfile As Boolean
        hasfile = ConnecAras.DocHasFileTest(ItemNumber.Label)
        If hasfile = True Then
            result = ConnecAras.UploadUpdatedDoc(ItemNumber.Label, FilepathComplete, ActiveDoc.Variables("DocName").Value + ".docx")
        Else
            result = ConnecAras.UploadNewDoc(ItemNumber.Label, FilepathComplete, ActiveDoc.Variables("DocName").Value + ".docx")
        End If

        'MsgBox(result)


    End Sub

    Private Sub UnLock_Click_1(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles UnLock.Click



        'verifier si le document est sauvegardé
        Dim DocIsSaved As Boolean = False
        Dim ConnecAras As New ConnecteurAras(ArasUrl, ArasDB, ArasUser, ArasPass)
        Dim result As String
        Dim NewItemID As String
        NewItemID = ItemNumber.Label

        'verifier si le document a déjà un fichier
        Dim DocHasFile As Boolean
        If DocIsSaved = True Then
            DocHasFile = True
        Else
            DocHasFile = ConnecAras.DocHasFileTest(ItemNumber.Label)
        End If

        'si non sauvegardé proposition de sauvegarder
        If (DocIsSaved = False) Then
            Dim QuestionResult As Integer
            QuestionResult = MsgBox("Do you want to Save this version before unlocking the document?", MsgBoxStyle.YesNoCancel, "confirm saving the doc")
            If QuestionResult = DialogResult.Yes Then
                ' save + upload + unlock
                Dim FilepathComplete As String = UserLocalDir + "\" + ActiveDoc.Variables("DocName").Value
                ActiveDoc.SaveAs(FilepathComplete)
                If DocHasFile = True Then
                    NewItemID = ConnecAras.UploadUpdatedDoc(ItemNumber.Label, FilepathComplete, ActiveDoc.Variables("DocName").Value)
                    ItemNumber.Label = NewItemID
                    result = ConnecAras.UnlockDoc(ItemNumber.Label)
                Else
                    NewItemID = ConnecAras.UploadNewDoc(ItemNumber.Label, FilepathComplete + ".docx", ActiveDoc.Variables("DocName").Value + ".docx")
                    result = ConnecAras.UnlockDoc(ItemNumber.Label)
                End If
                'MsgBox(result)
                
                MsgBox(result)
                Lock.Enabled = True
                Save.Enabled = False
                UnLock.Enabled = False

            ElseIf QuestionResult = DialogResult.No Then
                result = ConnecAras.UnlockDoc(ItemNumber.Label)
                MsgBox(result)
                Lock.Enabled = True
                Save.Enabled = False
                UnLock.Enabled = False
                ' unlock
            Else
                ' cancel

            End If

        End If

    End Sub

 
End Class
